package com.xoa.dev.pz.service.impl;

import com.xoa.dao.common.SysCodeMapper;
import com.xoa.dao.party.GridInfoMapper;
import com.xoa.dev.pz.dao.MenuJdtMapper;
import com.xoa.dev.pz.dao.PzPersonMapper;
import com.xoa.dev.pz.dao.PzKeyPetitionMapper;
import com.xoa.model.common.SysCode;
import com.xoa.model.enclosure.Attachment;
import com.xoa.model.party.GridInfo;
import com.xoa.dev.pz.model.PzPersonWithBLOBs;
import com.xoa.dev.pz.model.PzKeyPetition;
import com.xoa.dev.pz.model.PzKeyPetitionWithBLOBs;
import com.xoa.model.users.Users;
import com.xoa.service.sys.SysLogService;
import com.xoa.service.users.UsersService;
import com.xoa.util.DateFormat;
import com.xoa.util.ExcelUtil;
import com.xoa.util.GetAttachmentListUtil;
import com.xoa.util.ToJson;
import com.xoa.util.common.L;
import com.xoa.util.common.StringUtils;
import com.xoa.util.common.log.FileUtils;
import com.xoa.util.common.session.SessionUtils;
import com.xoa.util.netdisk.CheckAll;
import com.xoa.util.page.PageParams;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by gsb on 2017/12/02.
 */
@Service
public class PzKeyPetitionService {
    @Resource
    private PzKeyPetitionMapper pzKeyPetitionMapper;
    @Resource
    private PzPersonMapper pzPersonMapper;
    @Resource
    private UsersService usersService;
    @Resource
    private SysLogService sysLogService;
    @Resource
    private SysCodeMapper sysCodeMapper;
    @Resource
    GridInfoMapper gridInfoMapper;
    @Resource
    private MenuJdtMapper menuJdtMapper;
    /**
     * 创建人：桂静文
     * 创建时间：2017-12-02 下午15:26:17
     * 方法介绍：重点信访人员
     * 参数说明：
     */
    public ToJson<PzKeyPetitionWithBLOBs> insertSelective(HttpServletRequest request, PzKeyPetitionWithBLOBs record){
        ToJson<PzKeyPetitionWithBLOBs> json = new ToJson<>(1,"error");
        try{
            Users user= SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users());
            record.setCreateUser(user.getUserId());
            record.setCreateTime(DateFormat.getDatestr(new Date()));
            int temp=pzKeyPetitionMapper.insertSelective(record);
            sysLogService.setPzLog(request,user.getUserId(),new Date(),"1","6","重点信访人员");
            if(temp>0){
                json.setFlag(0);
                json.setMsg("ok");}
        }catch (Exception e){
            e.printStackTrace();
            json.setMsg("err");
            json.setFlag(1);
        }
        return json;
    }

    public ToJson<PzKeyPetitionWithBLOBs> updateByPrimaryKeySelective (HttpServletRequest request, PzKeyPetitionWithBLOBs record){
        ToJson<PzKeyPetitionWithBLOBs> json = new ToJson<>();
        try {
            Users user= SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users());
            L.s(record.getAttachmentId());
            L.s(record.getAttachmentName());
            int count = pzKeyPetitionMapper.updateByPrimaryKeySelective(record);

            sysLogService.setPzLog(request,user.getUserId(),new Date(),"3","6","重点信访人员");
            if(count > 0){
                json.setFlag(0);
                json.setMsg("ok");
            }else {
                json.setMsg("err");
                json.setFlag(1);
            }

        }catch (Exception e){
            e.printStackTrace();
            json.setMsg("err");
            json.setFlag(1);
        }
        return json;
    }

    public ToJson<PzKeyPetitionWithBLOBs> deleteByPrimaryKey(HttpServletRequest request, int keyPetitionsId){
        ToJson<PzKeyPetitionWithBLOBs> json = new ToJson<>();
        try {
            Users user= SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users());
            pzKeyPetitionMapper.deleteByPrimaryKey(keyPetitionsId);
            sysLogService.setPzLog(request,user.getUserId(),new Date(),"2","6","重点信访人员");
            json.setFlag(0);
            json.setMsg("ok");

        }catch (Exception e){
            e.printStackTrace();
            json.setMsg("err");
            json.setFlag(1);
        }
        return json;
    }

    public ToJson<PzKeyPetitionWithBLOBs> queryPzKeyPetition(HttpServletRequest request, HttpServletResponse response, PzKeyPetitionWithBLOBs pzKeyPetitionWithBLOBs, Integer page, Integer pageSize, boolean useFlag, String export, String pCradId, String pName){
        ToJson<PzKeyPetitionWithBLOBs> json = new ToJson<PzKeyPetitionWithBLOBs>(1,"err");
        PageParams pageParams=new PageParams();
        pageParams.setPage(page);
        pageParams.setPageSize(pageSize);
        pageParams.setUseFlag(useFlag);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("page", pageParams);
        map.put("pName",pName);
        map.put("pCradId",pCradId);
        Map<String, Object> param = new HashMap<String, Object>();
        Users user= SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users());
        param.put("userId", user.getUserId());
        param.put("userPriv", user.getUserPriv());
        param.put("deptId", user.getDeptId());
        String menuModel=menuJdtMapper.selMenuByModel("4");
        if(!CheckAll.checkAll(menuModel,param)){
            pzKeyPetitionWithBLOBs.setCreateUser(user.getUserId());
        }
        map.put("pzKeyPetition",pzKeyPetitionWithBLOBs);

        try{
            if(export==null){
                export="0";
            }
            List<PzKeyPetitionWithBLOBs> pzKeyPetitionList =  pzKeyPetitionMapper.queryPzKeyPetition(map);
            for(PzKeyPetitionWithBLOBs pzKeyPetitionWithBLOBs1 : pzKeyPetitionList){
                SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");
                SysCode sysCode = sysCodeMapper.getSingleCode("RENT_CONCERN", pzKeyPetitionWithBLOBs1.getDegreeConcern());
                if(sysCode!=null){
                    pzKeyPetitionWithBLOBs1.setDegreeConcernStr(sysCode.getCodeName());
                }
                SysCode sysCode1 = sysCodeMapper.getSingleCode("STATUS_PETITIONS", pzKeyPetitionWithBLOBs1.getStatusPetitions());
                if(sysCode1!=null){
                    pzKeyPetitionWithBLOBs1.setStatusPetitionsStr(sysCode1.getCodeName());
                }
                SysCode sysCode2 = sysCodeMapper.getSingleCode("CATEGORY_PETITIONS",pzKeyPetitionWithBLOBs1.getCategoryPetitions());
                if(sysCode2!=null){
                    pzKeyPetitionWithBLOBs1.setCategoryPetitionsStr(sysCode2.getCodeName());
                }

                if ("0".equals(pzKeyPetitionWithBLOBs1.getpSex())) {
                    pzKeyPetitionWithBLOBs1.setpSexStr("男");
                } else {
                    pzKeyPetitionWithBLOBs1.setpSexStr("女");
                }
                if(pzKeyPetitionWithBLOBs1.getpDate()!=null){
                    pzKeyPetitionWithBLOBs1.setpDateStr(sdf.format(pzKeyPetitionWithBLOBs1.getpDate()));
                }

                if ("01".equals(pzKeyPetitionWithBLOBs1.getpMaritalStatus())) {
                    pzKeyPetitionWithBLOBs1.setpMaritalStatusStr("未婚");
                } else {
                    pzKeyPetitionWithBLOBs1.setpMaritalStatusStr("已婚");
                }
                SysCode sysCode3 = sysCodeMapper.getSingleCode("POLITICAL", pzKeyPetitionWithBLOBs1.getpPolitical());
                if(sysCode3!=null){
                    pzKeyPetitionWithBLOBs1.setpPoliticalStr(sysCode3.getCodeName());
                }
                SysCode sysCode4 = sysCodeMapper.getSingleCode("DEGREE", pzKeyPetitionWithBLOBs1.getpEducation());
                if(sysCode4!=null){
                    pzKeyPetitionWithBLOBs1.setpEducationStr(sysCode4.getCodeName());
                }
                if ("0".equals(pzKeyPetitionWithBLOBs1.getpVehicles())) {
                    pzKeyPetitionWithBLOBs1.setpVehiclesStr("无");
                } else {
                    pzKeyPetitionWithBLOBs1.setpVehiclesStr("有");
                }
                SysCode sysCode5 = sysCodeMapper.getSingleCode("STATUS_PETITIONS", pzKeyPetitionWithBLOBs1.getpEducation());
                if(sysCode5!=null){
                    pzKeyPetitionWithBLOBs1.setpEducation(sysCode5.getCodeName());
                }
                SysCode sysCode6 = sysCodeMapper.getSingleCode("CATEGORY_PETITIONS", pzKeyPetitionWithBLOBs1.getpEducation());
                if(sysCode6!=null){
                    pzKeyPetitionWithBLOBs1.setpEducation(sysCode6.getCodeName());
                }

            }
            json.setTotleNum(pageParams.getTotal());
            json.setObj(pzKeyPetitionList);
            json.setFlag(0);
            json.setMsg("ok");
            if(export.equals("1")){
                HSSFWorkbook workbook1 = ExcelUtil.makeExcelHead("重点信访人员信息导出", 31);
                String[] secondTitles = {"所属网格","姓名","性别","身份证", "曾用名/别名", "出生日期","民族","联系方式","政治面貌","文化程度","婚姻状况","宗教信仰","职业","工作单位","户籍地址","籍贯","户籍地详址","现居地址","名下机动车辆","车牌号","备注", "创建人","关注程度","上访状态","上访原因","上访类别"};
                HSSFWorkbook workbook2 = ExcelUtil.makeSecondHead(workbook1, secondTitles);
                String[] beanProperty = {"gridName","pName","pSexStr","pCradId", "pAlias","pDateStr","pNation","pPhone","pPoliticalStr","pEducationStr","pMaritalStatusStr","pReligion","pOccupation","pWorkUnit","pDomicile","pOrigin","pDetailedDomicile","pAddress","pVehiclesStr","str1","pRemarks", "createUser", "degreeConcernStr","statusPetitionsStr","reasonsPetitions","categoryPetitionsStr"};
                HSSFWorkbook workbook = null;
                workbook = ExcelUtil.exportExcelData(workbook2, pzKeyPetitionList, beanProperty);
                OutputStream out = response.getOutputStream();
                String filename = "重点信访人员信息记录表.xls";
                filename = FileUtils.encodeDownloadFilename(filename, request.getHeader("user-agent"));
                response.setContentType("application/vnd.ms-excel;charset=UTF-8");
                response.setHeader("content-disposition", "attachment;filename=" + filename);
                workbook.write(out);
                out.close();
            }
        }catch (Exception e){
            e.printStackTrace();
            json.setMsg("err");
            json.setFlag(1);
        }
        return json;

    }

    public ToJson<PzKeyPetitionWithBLOBs> selectById(Integer keyPetitionsId, HttpServletRequest request){
        ToJson<PzKeyPetitionWithBLOBs> json = new ToJson<>();
        try {
            PzKeyPetitionWithBLOBs pzKeyPetitionWithBLOBs = pzKeyPetitionMapper.selectById(keyPetitionsId);
            SysCode sysCode = sysCodeMapper.getSingleCode("RENT_CONCERN", pzKeyPetitionWithBLOBs.getDegreeConcern());
            if(sysCode!=null){
                pzKeyPetitionWithBLOBs.setDegreeConcernStr(sysCode.getCodeName());
            }
            SysCode sysCode1 = sysCodeMapper.getSingleCode("STATUS_PETITIONS", pzKeyPetitionWithBLOBs.getStatusPetitions());
            if(sysCode1!=null){
                pzKeyPetitionWithBLOBs.setStatusPetitionsStr(sysCode1.getCodeName());
            }
            SysCode sysCode2 = sysCodeMapper.getSingleCode("CATEGORY_PETITIONS", pzKeyPetitionWithBLOBs.getCategoryPetitions());
            if(sysCode2!=null){
                pzKeyPetitionWithBLOBs.setCategoryPetitionsStr(sysCode2.getCodeName());
            }
            //附件处理
            List<Attachment> attachmentList=new ArrayList<Attachment>();
            if(pzKeyPetitionWithBLOBs.getAttachmentName()!=null&&!"".equals(pzKeyPetitionWithBLOBs.getAttachmentName())){
                attachmentList= GetAttachmentListUtil.returnAttachment(pzKeyPetitionWithBLOBs.getAttachmentName(),pzKeyPetitionWithBLOBs.getAttachmentId(),"xoa" + (String) request.getSession().getAttribute("loginDateSouse"), GetAttachmentListUtil.MODULE_PZ_PETITION);
            }
            pzKeyPetitionWithBLOBs.setAttachmentList(attachmentList);
            json.setObject(pzKeyPetitionWithBLOBs);
            json.setFlag(0);
            json.setMsg("ok");
        }catch (Exception e){
            e.printStackTrace();
            json.setMsg("err");
            json.setFlag(1);
        }
        return json;

    }


    @Transactional
    public ToJson<PzKeyPetitionWithBLOBs> importKeyPetition(HttpServletRequest request, HttpServletResponse response, MultipartFile file, HttpSession session) {
        ToJson<PzKeyPetitionWithBLOBs> toJson = new ToJson<PzKeyPetitionWithBLOBs>(1,"err");
        Users users= SessionUtils.getSessionInfo(request.getSession(),Users.class,new Users());
        // 成功次数
        Integer successCount = 0;
        try {
            //判读当前系统
            //读取配置文件
            ResourceBundle rb = ResourceBundle.getBundle("upload");
            String osName = System.getProperty("os.name");
            StringBuffer path = new StringBuffer();
            if (osName.toLowerCase().startsWith("win")) {
                path = path.append(rb.getString("upload.win"));
            } else {
                path = path.append(rb.getString("upload.linux"));
            }
            // 判断是否为空文件
            if (file.isEmpty()) {
                toJson.setMsg("请上传文件！");
                toJson.setFlag(1);
                return toJson;
            } else {
                String fileName = file.getOriginalFilename();
                if (fileName.endsWith(".xls") || fileName.endsWith(".xlsx")) {
                    String uuid = UUID.randomUUID().toString().replaceAll("-", "");
                    int pos = fileName.indexOf(".");
                    String extName = fileName.substring(pos);
                    String newFileName = uuid + extName;
                    File dest = new File(path.toString(), newFileName);
//                    file.transferTo(dest);
                    // 将文件上传成功后进行读取文件
                    // 进行读取的路径
                  //  String readPath = path.append(newFileName).toString();
                    String readPath = path.append(System.getProperty("file.separator")).append(newFileName).toString();
//                    InputStream in = new FileInputStream(readPath);
                    InputStream in =  file.getInputStream();
                    HSSFWorkbook excelObj = new HSSFWorkbook(in);
                    HSSFSheet sheetObj = excelObj.getSheetAt(0);
                    Row row = sheetObj.getRow(0);
                    int colNum = row.getPhysicalNumberOfCells();
                    int lastRowNum = sheetObj.getLastRowNum();
                    List<PzKeyPetitionWithBLOBs> saveList = new ArrayList<PzKeyPetitionWithBLOBs>();
                    List<PzPersonWithBLOBs> saveList1 = new ArrayList<PzPersonWithBLOBs>();
                    PzKeyPetitionWithBLOBs pzKeyPetitionWithBLOBs = null;
                    PzPersonWithBLOBs pzPersonWithBLOBs = null;
                    SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
                    int count = 0;
                    for (int i = 1; i <= lastRowNum; i++) {
                        pzKeyPetitionWithBLOBs = new PzKeyPetitionWithBLOBs();
                        pzPersonWithBLOBs = new PzPersonWithBLOBs();
                        row = sheetObj.getRow(i);
                        String value = "";
                        if (row != null) {
                            for (int j = 0; j <= colNum; j++) {
                                Cell cell = row.getCell(j);
                                if (cell != null) {
                                    switch (j) {
                                        case 0:
                                            //所属网格
                                            String as = cell.getStringCellValue();
                                            GridInfo info = gridInfoMapper.selectByUGridName(as);
                                            if(info!=null){

                                                pzPersonWithBLOBs.setpGrid(info.getGridId());
                                                pzKeyPetitionWithBLOBs.setGridId(info.getGridId());
                                            }
                                            break;
                                        case 1:
                                            // 姓名
                                            pzPersonWithBLOBs.setpName(cell.getStringCellValue());
                                            break;
                                        case 2:
                                            // 身份证
                                            pzPersonWithBLOBs.setpCradId(cell.getStringCellValue());
                                            break;
                                        case 3:
                                            // 别名
                                            pzPersonWithBLOBs.setpAlias(cell.getStringCellValue());
                                            break;
                                        case 4:
                                            // 性别
                                            if (cell.getStringCellValue().equals("女") ) {
                                                pzPersonWithBLOBs.setpSex("1");
                                            } else if (cell.getStringCellValue().equals("男") ) {
                                                pzPersonWithBLOBs.setpSex("0");
                                            }
                                            break;
                                        case 5:
                                            // 出生日期
                                            cell.setCellType(CellType.STRING);
                                            pzPersonWithBLOBs.setpDate(sd.parse(cell.getStringCellValue().trim()));
                                            break;
                                        case 6:
                                            // 民族
                                            pzPersonWithBLOBs.setpNation(cell.getStringCellValue());
                                            break;
                                        case 7:
                                            // 联系方式
                                            if (cell.getCellType() == CellType.NUMERIC) {
                                                BigDecimal bigP=new BigDecimal(cell.getNumericCellValue());
                                                value = bigP.toString();
                                                //解决1234.0  去掉后面的.0
                                                if(null!=value&&!"".equals(value.trim())){
                                                    String[] item = value.split("[.]");
                                                    if(1<item.length&&"0".equals(item[1])){
                                                        value=item[0];
                                                    }
                                                }
                                                pzPersonWithBLOBs.setpPhone(value);
                                            }else {
                                                pzPersonWithBLOBs.setpPhone(cell.getStringCellValue());
                                            }
                                            break;
                                        case 8:
                                            // 政治面貌
                                            pzPersonWithBLOBs.setpPolitical((sysCodeMapper.getByName("POLITICAL",cell.getStringCellValue().trim())).getCodeNo());
                                            break;
                                        case 9:
                                            // 文化程度
                                            pzPersonWithBLOBs.setpEducation((sysCodeMapper.getByName("DEGREE",cell.getStringCellValue().trim())).getCodeNo());
                                            break;
                                        case 10:
                                            // 婚姻状况
                                            if (cell.getStringCellValue() .equals("未婚") ) {
                                                pzPersonWithBLOBs.setpMaritalStatus("01");
                                            } else if (cell.getStringCellValue().equals("已婚") ) {
                                                pzPersonWithBLOBs.setpMaritalStatus("02");
                                            }
                                            break;
                                        case 11:
                                            //宗教信仰
                                            pzPersonWithBLOBs.setpReligion(cell.getStringCellValue());
                                            break;
                                        case 12:
                                            //职业
                                            pzPersonWithBLOBs.setpOccupation(cell.getStringCellValue());
                                            break;
                                        case 13:
                                            //工作单位
                                            pzPersonWithBLOBs.setpWorkUnit(cell.getStringCellValue());
                                            break;
                                        case 14:
                                            //户籍地址
                                            pzPersonWithBLOBs.setpDomicile(cell.getStringCellValue());
                                            break;
                                        case 15:
                                            //籍贯
                                            pzPersonWithBLOBs.setpOrigin(cell.getStringCellValue());
                                            break;
                                        case 16:
                                            //户籍地详址
                                            pzPersonWithBLOBs.setpDetailedDomicile(cell.getStringCellValue());
                                            break;
                                        case 17:
                                            //名下机动车辆
                                            if (cell.getStringCellValue() .equals("有")) {
                                                pzPersonWithBLOBs.setpVehicles("1");
                                            } else if (cell.getStringCellValue() .equals("无") ) {
                                                pzPersonWithBLOBs.setpVehicles("0");
                                            }
                                            break;
                                        case 18:
                                            //备注
                                            pzPersonWithBLOBs.setpRemarks(cell.getStringCellValue());
                                            break;
                                        case 19:
                                            //有无固定住所
                                            if (cell.getStringCellValue() .equals("无") ) {
                                                pzPersonWithBLOBs.setpIsHouse("0");
                                            } else if (cell.getStringCellValue() .equals("有") ) {
                                                pzPersonWithBLOBs.setpIsHouse("1");
                                            }
                                            break;
                                        case 20:
                                            //现居地址
                                            pzPersonWithBLOBs.setpAddress(cell.getStringCellValue());
                                            break;
                                        case 21:
                                            //实口类型
                                                if (cell.getStringCellValue() .equals("流动人口") ) {
                                                pzPersonWithBLOBs.setFlag1("2");
                                            } else if (cell.getStringCellValue() .equals("户籍人口") ) {
                                                pzPersonWithBLOBs.setFlag1("1");
                                            }
                                            break;
                                        case 22:
                                            //车牌号
                                            cell.setCellType(CellType.STRING);
                                            pzPersonWithBLOBs.setStr1(cell.getStringCellValue());
                                            break;
                                        case 23:
                                            // 关注程度
                                            pzKeyPetitionWithBLOBs.setDegreeConcern((sysCodeMapper.getByName("RENT_CONCERN",cell.getStringCellValue().trim())).getCodeNo());
                                            break;
                                        case 24:
                                            // 上访状态
                                            pzKeyPetitionWithBLOBs.setStatusPetitions((sysCodeMapper.getByName("STATUS_PETITIONS",cell.getStringCellValue().trim())).getCodeNo());
                                            break;
                                        case 25:
                                            // 上访原因
                                            pzKeyPetitionWithBLOBs.setReasonsPetitions(cell.getStringCellValue());
                                            break;
                                        case 26:
                                            // 上访类别
                                            pzKeyPetitionWithBLOBs.setCategoryPetitions((sysCodeMapper.getByName("CATEGORY_PETITIONS",cell.getStringCellValue().trim())).getCodeNo());
                                            break;
                                        default:
                                            toJson.setFlag(0);
                                            toJson.setMsg("err");
                                            break;
                                    }
                                }

                            }
                            List<String> nullArray = new ArrayList();
                            if (pzPersonWithBLOBs.getpGrid() == null) {
                                nullArray.add("特殊网格");
                            }
                            if (pzPersonWithBLOBs.getpName() == null) {
                                nullArray.add("姓名");
                            }
                            if (pzPersonWithBLOBs.getpSex() == null) {
                                nullArray.add("性别");
                            }
                            if (pzPersonWithBLOBs.getpCradId() == null) {
                                nullArray.add("身份证号");
                            }
                            if (pzPersonWithBLOBs.getFlag1() == null) {
                                nullArray.add("实口类型");
                            }
                            if (pzPersonWithBLOBs.getpOrigin() == null) {
                                nullArray.add("籍贯");
                            }
                            if (pzPersonWithBLOBs.getpIsHouse() == null) {
                                nullArray.add("有无固定住所");
                            }
                            if (pzPersonWithBLOBs.getpAddress() == null) {
                                nullArray.add("现居地址");
                            }
                            if (pzPersonWithBLOBs.getpMaritalStatus() == null) {
                                nullArray.add("婚姻状况");
                            }
                            if (pzPersonWithBLOBs.getpDomicile() == null) {
                                nullArray.add("户籍地址");
                            }
                            if (pzPersonWithBLOBs.getpDetailedDomicile() == null) {
                                nullArray.add("户籍地详情");
                            }
                            if (pzPersonWithBLOBs.getpVehicles() == null) {
                                nullArray.add("名下机动车辆");
                            }
                            if(pzKeyPetitionWithBLOBs.getCategoryPetitions()==null){
                                nullArray.add("上访类别");
                            }
                            if(pzKeyPetitionWithBLOBs.getReasonsPetitions()==null){
                                nullArray.add("上访原因");
                            }
                            if (nullArray.size() > 0) {
                                toJson.setObject(nullArray);
                                toJson.setMsg("必填字段不能为空，请重新导入");
                            } else {
                                //判断用户是否为空，不为空判断用户是否存在
                                if (!StringUtils.checkNull(pzPersonWithBLOBs.getpName()) && !StringUtils.checkNull(pzPersonWithBLOBs.getpCradId())) {
                                    Map<String, Object> map = new HashMap<String, Object>();
                                    map.put("pCradId", pzPersonWithBLOBs.getpCradId());
                                    map.put("pName", pzPersonWithBLOBs.getpName());
                                    PzPersonWithBLOBs pzPersons = pzPersonMapper.selectPersonCountByNameCrad(map);
                                    if (pzPersons.getCount() > 0) {
                                        pzPersonWithBLOBs.setpId(pzPersons.getpId());
                                        pzPersonWithBLOBs.setFlag2("0");
                                        if(!pzPersons.getSpecialStr().contains("3,")){
                                            String str = "3," + pzPersons.getSpecialStr();
                                            pzPersonWithBLOBs.setSpecialStr(str);
                                        }
                                        pzKeyPetitionWithBLOBs.setUserId(String.valueOf(pzPersons.getpId()));
                                        pzPersonMapper.updateByPrimaryKeySelective(pzPersonWithBLOBs);
                                    } else {
                                        //创建用户和创建时间
                                        pzPersonWithBLOBs.setCreateUser(users.getUserId());
                                        pzPersonWithBLOBs.setCreateTime(DateFormat.getCurrentTime());
                                        pzPersonWithBLOBs.setFlag2("0");
                                        pzPersonWithBLOBs.setSpecialStr("3,");
                                        pzPersonMapper.insertSelective(pzPersonWithBLOBs);
                                        pzKeyPetitionWithBLOBs.setUserId(String.valueOf(pzPersonWithBLOBs.getpId()));
                                    }
                                }
                                //将数据放入数据库
                                //用户表与肇事表关联用户userid
                                pzKeyPetitionWithBLOBs.setUserId(String.valueOf(pzPersonWithBLOBs.getpId()));
                                //判断关联用户userid是否为空,为空新增,不为空修改
                                if (!StringUtils.checkNull(pzKeyPetitionWithBLOBs.getUserId())) {
                                    //如果不为空，查询是否有肇事信息，有则更新，否则新增
                                    Map<String, Object> map = new HashMap<String, Object>();
                                    map.put("userId", pzKeyPetitionWithBLOBs.getUserId());
                                    PzKeyPetition pzKeyP = pzKeyPetitionMapper.selectByUserId(map);
                                    if (pzKeyP.getCou() > 0) {
                                        pzKeyPetitionWithBLOBs.setKeyPetitionsId(pzKeyP.getKeyPetitionsId());
                                        count += pzKeyPetitionMapper.updateByPrimaryKeySelective(pzKeyPetitionWithBLOBs);
                                    } else {
                                        //创建用户和创建时间
                                        pzKeyPetitionWithBLOBs.setCreateUser(users.getUserId());
                                        pzKeyPetitionWithBLOBs.setCreateTime(DateFormat.getCurrentTime());
                                        count += pzKeyPetitionMapper.insertSelective(pzKeyPetitionWithBLOBs);
                                    }
                                }
                             }
                        }
                    }
                    if (count > 0) {
                        successCount = successCount + count;
                    }
                    dest.delete();
                    if (successCount > 0) {
                        toJson.setTotleNum(successCount);
                        toJson.setMsg("ok");
                        toJson.setFlag(0);
                        return toJson;
                    }

                }
            }
        } catch (Exception e) {
            //读取配置文件,获取文件要存储的路径
            StringBuffer path = this.getPath();
            String fileName = file.getOriginalFilename();
            if (fileName.endsWith(".xls") || fileName.endsWith(".xlsx")) {
                String uuid = UUID.randomUUID().toString().replaceAll("-", "");
                String extName = fileName
                        .substring(fileName.lastIndexOf(".") + 1);// 文件后缀
                String newFileName = uuid + extName;//组合成新的文件名
                File dest = new File(path.toString(), newFileName);
                dest.delete();
            }
            e.getMessage();
            e.printStackTrace();
        }

        return toJson;
    }

    //判读当前系统,读取配置文件,返回路径
    public StringBuffer getPath(){
        ResourceBundle rb = ResourceBundle.getBundle("upload");
        String osName = System.getProperty("os.name");
        StringBuffer path = new StringBuffer();
        if (osName.toLowerCase().startsWith("win")) {
            path = path.append(rb.getString("upload.win"));
        } else {
            path = path.append(rb.getString("upload.linux"));
        }
        return path;
    }
}
